<?php

/**
 * Accesses the tgp_accounts table in the thegridpaper database
 * @author panosbaroudjian
 *
 */
class Application_Model_DbTable_Accounts extends Zend_Db_Table_Abstract
{
    protected $_name = 'tgp_accounts';
    protected $_primary = 'account_id';
    protected $_dependentTables = array('Application_Model_DbTable_AccountFeeds',
                                        'Application_Model_DbTable_AccountTokens',
                                        'Application_Model_DbTable_GridBoxes');

    /**
     * Gets an account by its id
     * @param int $id The id of the account
     * @return NULL|array An array containing the account's row,
     * NULL if the account is not found.
     */
    public function getById($id)
    {
        $id = (int)$id;
        $row = $this->find((int)$id);
        if (!$row || $row->count() == 0)
        {
            return null;
        }
        return $row->current()->toArray();
    }

    /**
     * Gets an account by its OpenID
     * @param string $openid The OpenID of the account
     * @return NULL|Array An array containing the account's row,
     * NULL if the account is not found.
     */
    public function getByOpenId($openid)
    {
        $select = $this->select()->where('openid = ?', $openid);
        $row = $this->fetchRow($select);
        if (!$row)
        {
            return null;
        }
        return $row->toArray();
    }

    /**
     * Gets an account by its email
     * @param string $email The email of the account
     * @return NULL|Array An array containing the account's row,
     * NULL if the account is not found.
     */
    public function getByEmail($email)
    {
        $select = $this->select();
        $select->where('email = ?', $email);
        $row = $this->fetchRow($select);
        if (!$row)
        {
            return null;
        }
        return $row->toArray();
    }

    /**
     * Gets the feeds registered with the account
     * @param int $account_id The id of the account
     * @return NULL|array: An array containing all the feed rows of the account, NULL otherwise.
     */
    public function getFeedsById($account_id)
    {
        $account_id = (int)$account_id;
        $rowSet = $this->find((int)$account_id);
        if (!$rowSet)
        {
            return null;
        }
        $row = $rowSet->current();
        $feedsRowSet = $row->findManyToManyRowset('Application_Model_DbTable_Feeds',
            'Application_Model_DbTable_AccountFeeds');
        return $feedsRowSet->toArray();
    }

    /**
     * Gets the registered tokens with the account
     * @param int $account_id The id of the account
     * @return NULL|array: An array containing all the token rows of the account, NULL otherwise.
     */
    public function getTokensById($account_id)
    {
        $account_id = (int)$account_id;
        $rowSet = $this->find((int)$account_id);
        if (!$rowSet)
        {
            return null;
        }
        $row = $rowSet->current();
        $tokensRowSet = $row->findManyToManyRowset('Application_Model_DbTable_Tokens',
            'Application_Model_DbTable_AccountTokens');
        return $tokensRowSet->toArray();
    }

    /**
     * Inserts a new tgp_accounts row
     * @param string $openid The OpenId of the account
     * @param string $email The email of the account
     * @param string $firstName The first name of the account
     * @param string $lastName The last name of the account
     * @param string $country The country of the account
     * @param string $language The language of the account
     * @param int $max_newnits The max number of newnits of the account
     * @return int: The id of the inserted row
     */
    public function insertAccount($openid, $email, $firstName, $lastName, $country, $language, $max_newnits)
    {
        $data = array('email' => $email,
                    'openid' => $openid,
                    'firstName' => $firstName,
                    'lastName' => $lastName,
                    'country' => $country,
                    'language' => $language,
                    'max_newnits' => $max_newnits);
        return $this->insert($data);
    }

    /**
     * Updates a row in the tgp_accounts table
     * @param int $id The id of the account
     * @param string $email The email of the account
     * @param string $firstName The first name of the account
     * @param string $lastName The last name of the account
     * @param string $country The country of the account
     * @param string $language The language of the account
     * @param int $max_newnits The max number of newnits of the account
     */
    public function updateAccount($id, $openid, $email, $firstName, $lastName, $country, $language, $max_newnits)
    {
        $where = $this->getAdapter()->quoteInto('account_id = ?', $id);
        $data = array('email' => $email,
                    'openid' => $openid,
                    'firstName' => $firstName,
                    'lastName' => $lastName,
                    'country' => $country,
                    'language' => $language,
                    'max_newnits' => $max_newnits);
        $this->update($data, $where);
    }

    /**
     * Updates the lastUpdateTime of the account
     * @param int $id The id of the account
     * @param int $lut The timestamp of the last update time
     */
    public function updateLastUpdateTime($id, $lut)
    {
        $where = $this->getAdapter()->quoteInto('account_id = ?', $id);
        $data = array('last_update_time' => $lut);
        $this->update($data, $where);
    }

    /**
     * Updates the token counts of the account
     * @param int $id The id of the account
     * @param int $pos_count The token_pos_count of the account
     * @param int $neg_count The token_neg_count of the account
     * @param int $total_count The total count of tokens of the account
     */
    public function updateTokenCounts($id, $pos_count, $neg_count, $total_count)
    {
        $where = $this->getAdapter()->quoteInto('account_id = ?', $id);
        $data = array('token_pos_count' => (int)$pos_count,
                    'token_neg_count' => (int)$neg_count,
                    'token_count' => (int)$total_count);
        $this->update($data, $where);
    }

    /**
     * Updates the max number of newnits of the account
     * @param int $id The id of the account
     * @param int $max_newnits The max number of newnits
     */
    public function updateMaxNewnits($id, $max_newnits)
    {
        $where = $this->getAdapter()->quoteInto('account_id = ?', $id);
        $data = array('max_newnits' => $max_newnits);
        $this->update($data, $where);
    }

    /**
     * Deletes an account from tgp_accounts
     * @param int $id The id of the account to be deleted
     */
    public function deleteAccount($id)
    {
        $where = $this->getAdapter()->quoteInto('account_id = ?', $id);
        $this->delete($where);
    }
}

